<template>
	<div class="sys-role-container">
		<MkDialog ref="dialogRef" @submit="submit" width="800px">
			<MkForm :model="state.ruleForm">
				<MkInput label="模块" v-model="state.ruleForm.menuTitle" />
			</MkForm>
			<el-tree ref="treeRef" :data="state.menuData" node-key="id" show-checkbox
				:props="{ children: 'children', label: 'title', class: treeNodeClass }" icon="ele-Menu"
				highlight-current default-expand-all style="height: 600px;overflow-y: auto;" />
		</MkDialog>
	</div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue';
import type { ElTree } from 'element-plus';
import { getAPI } from '/@/utils/axios-utils';
import { SysMenuApi, SysRoleApi } from '/@/api-services/api';
import { SysMenu } from '/@/api-services/models';
import { clog, clone, showSuccess, showWarning } from '/@/utils';
import { BasZhanDianMenuApi, BasZhanDianMenuTreeOutput } from '/@/api/bas/BasZhanDianMenu';

const dialogRef = ref();
const emits = defineEmits(['handleQuery']);
const treeRef = ref<InstanceType<typeof ElTree>>();
const state = reactive({
	isEdit: false,
	loading: false,
	ruleForm: {} as BasZhanDianMenuTreeOutput,
	menuData: [] as Array<SysMenu>, // 菜单数据
});

onMounted(async () => {
	state.loading = true;
	var res = await getAPI(SysMenuApi).apiSysMenuListGet();
	// 去掉第二级
	var menuList = res.data.result ?? []
	menuList.forEach(item => {
		item.children = []
	});
	state.menuData = menuList
	state.loading = false;
});

// 打开弹窗
const show = async (row: any) => {
	dialogRef.value.show();
	state.isEdit = false;
	treeRef.value?.setCheckedKeys([]); // 清空选中值
	state.ruleForm = clone(row);

	// 选中已经有的
	var checkedIdList = state.menuData.filter(item => row.children.some(item2 => item.id === item2.menuId));
	setTimeout(() => {
		treeRef.value?.setCheckedNodes(checkedIdList);
	}, 100);
};

// 提交
const submit = async () => {
	var list = treeRef.value?.getCheckedNodes();
	var listInput = list?.map(item => {
		return { zhanDian: state.ruleForm.menuTitle, menuId: item.id, remark: item.title };
	});
	clog(listInput);
	if (!listInput || listInput.length == 0) {
		showWarning("请选择菜单");
		return;
	}
	await BasZhanDianMenuApi.addRange(listInput);
	showSuccess();
	dialogRef.value.close();
	emits('handleQuery');
};

// 叶子节点同行显示样式
const treeNodeClass = (node: SysMenu) => {
	let addClass = true; // 添加叶子节点同行显示样式
	for (var key in node.children) {
		// 如果存在子节点非叶子节点，不添加样式
		if (node.children[key].children?.length ?? 0 > 0) {
			addClass = false;
			break;
		}
	}
	return addClass ? 'penultimate-node' : '';
};

// 导出对象
defineExpose({ show });
</script>

<style lang="scss" scoped>
.menu-data-tree {
	width: 100%;
	border: 1px solid var(--el-border-color);
	border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
	padding: 5px;
}

:deep(.penultimate-node) {
	.el-tree-node__children {
		padding-left: 40px;
		white-space: pre-wrap;
		line-height: 100%;

		.el-tree-node {
			display: inline-block;
		}

		.el-tree-node__content {
			padding-left: 5px !important;
			padding-right: 5px;
		}
	}
}
</style>
